package gui.newActions;

import javax.swing.*;
import java.util.ArrayList;
import java.awt.event.*;
import java.io.*;

public class KeyListTF implements KeyListener{

    ArrayList<String[]> autoreq = new ArrayList<String[]>();
    JTextField txt_req;
    boolean bool;

    KeyListTF(String file_path,JTextField treq) throws IOException {
        this.txt_req = treq;

        InputStreamReader user = new InputStreamReader(new FileInputStream(file_path),("cp1251"));

        boolean k = false;
        String tmp = "";
        String tmp2 = "";

        if (user != null){
            for (;user.ready() == true;){
                char c = (char) user.read();
                if (Integer.valueOf(c) != KeyEvent.VK_ENTER){
                    if (String.valueOf(c).equals(";") == true ){

                        if (k){k = false;}else{k = true;}

                        if (tmp != ""){
                            if (tmp2 != ""){
                                autoreq.add(new String[]{tmp,tmp2});
                                tmp = "";
                                tmp2 = "";
                            }
                        }

                    }else{
                        if (k){tmp = tmp + c;}
                        else{tmp2 = tmp2 + c;}
                    }
                }}
        }

    }

    public void keyTyped(KeyEvent e) {}

    public void keyPressed(KeyEvent e) {
        bool = false;
        int ind=0;
        String s1,s2;
        for (int i=0; i<autoreq.size(); i++){
            s1 = String.valueOf(e.getKeyChar());
            s2 = autoreq.get(i)[0];
            if (s1.equals(s2)){
                bool = true;
                ind = i;
            }
        }
        if (bool){
            txt_req.setText(txt_req.getText()+" "+autoreq.get(ind)[1]);

        }else{
        }
    }

    public void keyReleased(KeyEvent e) {
    }

}
